// 显示底部的菜单, 慢慢向上和慢慢向下
function show_menu () {
    var menu = $('stat_panel_contain');
    var temp = $('stat_panel_menu');
    if (menu.clientWidth == 0 || menu.style.display == 'none')
        show_menu_li ('up');
    else {
        temp.style.marginTop = '130px';
        show_menu_li ('down');
        return ;
    }

    function show_menu_li (type){
        var num  = 10;
        var height = 130;
        var step = 50;
        var i = 1;
        function move_up () {
            temp.style.marginTop = parseInt(height - i * (height/10)) + 'px';
            i ++;
            if (i > 10 || fn2) {
                clearInterval (fn1);
                return ;
            }
        }
        function move_down() {
            temp.style.marginTop = parseInt( i * height/10 ) + 'px';
            i ++;
            if (i > 10 || fn1) {
                clearInterval (fn2);
                hide_it (menu);
                return ;
            }
        }
        if (type == 'up') {
            show_it (menu);
            var fn1 = setInterval (move_up, step);
        }
        else
            var fn2 = setInterval (move_down,step);
    }  // end function show_menu_li
}
    
// 处理返回查找用户信息
function search_user_after (text) {
    var json = toJSON (text);
    var temp = $('manage_friends_text');
    if (! json.status) {
        temp.innerHTML = json.text;
        return ;
    }
    var user = toJSON (json.text);
    var sex  = (user.sex == 'm') ? '男' : '女';
    var template = '<div style="width:95%; overflow:hidden; border-bottom:1px solid #EEAEEE;margin:6px;"> \
        {0}&nbsp;&nbsp; ({1})&nbsp;&nbsp;{2}&nbsp;&nbsp;//{3} </div>  \
        <form onsubmit="add_friend(this);return false">附言: <input type="text" style="width:270px"> \
        <input type="hidden" value="{4}">  \
        <input type="submit" class="button1" value="添加为好友" ></form> ';
    var s = String.format (template,user.name,user.nickname,sex,user.description, user.id);
    temp.innerHTML = s;
}

// 发送查找用户信息请求
function search_user (temp) {
    var inputs = temp.getElementsByTagName('input');
    var user   = inputs[0].value.trim();
    var text   = $('manage_friends_text');
    text.innerHTML = '&nbsp;';
    if (! user)
        text.innerHTML = '用户名不能空';
    else {
        var url = './script/find_friend.php?user=' + user;
        xhr_get ( search_user_after, url);
    }
    return false;
}

// 显示请求添加好友的结果
function add_friend_after (text) {
    var json = toJSON(text);
    show_message_text (json.text);
}

// 请求添加为好友
function add_friend (temp) {
    var inputs  = temp.getElementsByTagName ('input');
    var word = inputs[0].value.trim();
    var user = inputs[1].value.trim();
    var url  = "./script/add_friend.php?msg=" + word + "&f_id=" + user;
    xhr_get (add_friend_after, url);
}

// 将一条消息显示在 消息栏中
function show_message_text (text) {
    var msg = $('message');
    show_it (msg);
    msg = $('message_top');
    var msg_text = $('message_text');
    var height = msg.clientHeight;
    var slice = 100;
    msg_text.style.marginTop = height + "px";
    msg_text.innerHTML = '[消息] // ' + text;
    var i = 1, num=10;
    function move_up () {
        if (i > num) {
            clearInterval (fn);
            return;
        } 
        msg_text.style.marginTop = height-(height/num)*i;
        i++;
    }
    msg.style.left = middle(msg).x + 'px';
    var fn = setInterval (move_up, slice);
}
